package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DataInsulinEntity;
import com.ywjh.inter.utils.StrUtil;

public class DataInsulinDAO {

	public boolean insert(Connection conn, DataInsulinEntity e) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"insert into plan_insulin(doc_id,add_time,data,type,memo,check_time,status,patient_id) values(?,?,?,?,?,?,?,?)");
		ps.setInt(1, e.getDoc_id());
		ps.setTimestamp(2, e.getAdd_time());
		ps.setFloat(3, e.getData());
		ps.setInt(4, e.getType());
		ps.setString(5, e.getMemo());
		ps.setTimestamp(6, e.getCheck_time());
		ps.setInt(7, e.getStatus());
		ps.setInt(8, e.getPatient_id());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	public boolean update(Connection conn, DataInsulinEntity e) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update plan_insulin set doc_id=?,add_time=?,data=?,type=?,memo=?,check_time=?,status=?,patient_id=? where id =?");
		ps.setInt(1, e.getDoc_id());
		ps.setTimestamp(2, e.getAdd_time());
		ps.setFloat(3, e.getData());
		ps.setInt(4, e.getType());
		ps.setString(5, e.getMemo());
		ps.setTimestamp(6, e.getCheck_time());
		ps.setInt(7, e.getStatus());
		ps.setInt(8, e.getPatient_id());
		ps.setInt(9, e.getId());
		if (ps.executeUpdate() > 0) {
			b = true;
		}
		return b;
	}

	private DataInsulinEntity fill(ResultSet rs) throws SQLException {
		DataInsulinEntity d = new DataInsulinEntity();
		d.setId(rs.getInt("id"));
		d.setAdd_time(rs.getTimestamp("add_time"));
		d.setCheck_time(rs.getTimestamp("check_time"));
		d.setData(rs.getFloat("data"));
		d.setDoc_id(rs.getInt("doc_id"));
		d.setMemo(rs.getString("memo"));
		d.setPatient_id(rs.getInt("patient_id"));
		d.setStatus(rs.getInt("status"));
		d.setType(rs.getInt("type"));
		return d;
	}

	public List<DataInsulinEntity> query(Connection conn, String start_time, String end_time, int patient_id, int type)
			throws SQLException {
		List<DataInsulinEntity> list = new LinkedList<DataInsulinEntity>();
		PreparedStatement ps = conn.prepareStatement(
				"select * from plan_insulin where patient_id=? and date_format(check_time,'%y%m%d') >= date_format(?,'%y%m%d') and date_format(check_time,'%y%m%d') < date_format(?,'%y%m%d') and type=? and status =1 order by check_time desc,memo");
		ps.setInt(1, patient_id);
		ps.setString(2, start_time);
		ps.setString(3, end_time);
		ps.setInt(4, type);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			list.add(fill(rs));
		}
		return list;
	}

	public boolean disableDataByType(Connection conn, int patient_id, int type,String time) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
				"update plan_insulin set status=0 where date_format(check_time,'%y%m%d') = date_format(?,'%y%m%d') and type=? and patient_id=?");
		ps.setString(1, StrUtil.getYYYYMMDD(new Timestamp(System.currentTimeMillis())));
		ps.setInt(2, type);
		ps.setInt(3, patient_id);
		ps.executeUpdate();

		return true;
	}

	public boolean deleteDataByType(Connection conn, int patient_id, int type,Timestamp checkTime) throws SQLException {
		PreparedStatement ps = conn.prepareStatement(
				"delete from plan_insulin where date_format(check_time,'%y%m%d') = date_format(?,'%y%m%d') and type=? and patient_id=?");
		ps.setString(1, StrUtil.getYYYYMMDD(checkTime));
		ps.setInt(2, type);
		ps.setInt(3, patient_id);
		ps.executeUpdate();

		return true;
	}

	public DataInsulinEntity findByTypeTime(Connection conn, int patient_id, int type, Timestamp ts,String memo) throws SQLException {
		DataInsulinEntity e = null;
		PreparedStatement ps = conn.prepareStatement(
				"select * from plan_insulin where patient_id=? and check_time=? and type=? and status =1 and memo=?");
		ps.setInt(1, patient_id);
		ps.setTimestamp(2, ts);
		ps.setInt(3, type);
		ps.setString(4, memo);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			e = fill(rs);
		}
		return e;
	}

}
